﻿using Caliburn.Micro;
using myapp.Databases.DAO;
using myapp.Events;
using myapp.Events.ViewModel;
using myapp.Instances;
using myapp.ViewModels;
using myappcore.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myapp.ViewModels
{
    /// <summary>
    /// Group of Possition
    /// </summary>
    public class PossitionGroupListViewModel : IBaseContentViewModel, IHandle<ViewModelPossitionGroupSelectedEvent>
    {
        #region Attribute

        private IObservableCollection<PossitionGroupViewModel> _possitionGroupViewList;

        private static PossitionGroupListViewModel _instance;
        #endregion

        #region Properties

        /// <summary>
        /// List of Possition Group List
        /// </summary>
        public IObservableCollection<PossitionGroupViewModel> PossitionGroupViewList
        {
            get
            {
                if (null == _possitionGroupViewList)
                {
                    _possitionGroupViewList = new BindableCollection<PossitionGroupViewModel>();
                }
                if (IsChanged)
                {
                    _possitionGroupViewList.Clear();
                    foreach (PossitionGroup groupModel in PossitionGroupDao.ListPossitionGroup())
                    {
                        PossitionGroupViewModel groupView = new PossitionGroupViewModel()
                        {
                            PossitionGroup = groupModel,
                            CurrentColor = MainInstance.Instance.ColorConfig.PosstionGroupNormal
                        };
                        _possitionGroupViewList.Add(groupView);
                    }
                    if (_possitionGroupViewList.Count > 0)
                    {
                        IsChanged = false;
                    }
                }
                return _possitionGroupViewList;
            }
        }

        /// <summary>
        /// Get Instance of Possition Group View Model
        /// </summary>
        public static PossitionGroupListViewModel Instance
        {
            get
            {
                if (null == _instance)
                {
                    _instance = new PossitionGroupListViewModel();
                    _instance.IsChanged = true;                   
                }
                return _instance;
            }
        }
        #endregion

        #region Constructure
        public PossitionGroupListViewModel()
        {
            MainInstance.Instance.EventAggregator.Subscribe(this);
            DisplayName = "Danh Sách Khu Vực";
            CurrentColor = MainInstance.Instance.ColorConfig.PosstionGroupNormal;
        }

        #endregion

        #region Method       

        /// <summary>
        /// Process handle selected possition group
        /// </summary>
        /// <param name="message"></param>
        public void Handle(ViewModelPossitionGroupSelectedEvent message)
        {
            if (message != null && message.PossitionGroupViewModel != null)
            {
                message.PossitionGroupViewModel.CurrentColor = MainInstance.Instance.ColorConfig.PosstionGroupSelected;

                for (int i = 0; i < _possitionGroupViewList.Count; i++)
                {
                    if (_possitionGroupViewList[i].PossitionGroup != message.PossitionGroupViewModel.PossitionGroup)
                    {
                        if (_possitionGroupViewList[i].CurrentColor == MainInstance.Instance.ColorConfig.PosstionGroupSelected)
                        {
                            _possitionGroupViewList[i].CurrentColor = MainInstance.Instance.ColorConfig.PosstionGroupNormal;
                            break;
                        }
                    }
                }

            }
        }
        #endregion


    }
}
